 
// Source File Name:   SCImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.SC;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            STCommonImpl, ANYImpl, BLImpl, CEImpl, 
//            CSImpl, NullFlavorImpl, TYPEImpl

public class SCImpl extends STCommonImpl
    implements SC
{

    public static final String RCS_ID = "$Header: SCImpl.java 115.25 2005/09/16 09:23:36 amchanda noship $";
    protected CE _code;
    private static final SC nullNI = new SCImpl(CSImpl.valueOf("NI"));
    private static final SC nullOTH = new SCImpl(CSImpl.valueOf("OTH"));
    private static final SC nullNINF = new SCImpl(CSImpl.valueOf("NINF"));
    private static final SC nullPINF = new SCImpl(CSImpl.valueOf("PINF"));
    private static final SC nullUNK = new SCImpl(CSImpl.valueOf("UNK"));
    private static final SC nullASKU = new SCImpl(CSImpl.valueOf("ASKU"));
    private static final SC nullNAV = new SCImpl(CSImpl.valueOf("NAV"));
    private static final SC nullMSK = new SCImpl(CSImpl.valueOf("MSK"));
    private static final SC nullNA = new SCImpl(CSImpl.valueOf("NA"));
    private static final SC nullNASK = new SCImpl(CSImpl.valueOf("NASK"));

    public static SC valueOf(String s)
    {
        return valueOf(s, null);
    }

    public static SC valueOf(String s, CE ce)
    {
        if(s == null || s.length() == 0)
            return new SCImpl(NullFlavorImpl.NI);
        if(s.length() == 0)
        {
            if(ce == null || ce.isNull().isTrue())
                return new SCImpl(NullFlavorImpl.NI);
            else
                throw ANYImpl.newNullException("SC", "valueOf", "value");
        } else
        {
            return new SCImpl(s, ce);
        }
    }

    protected SCImpl(String s, CE ce)
    {
        super(s);
        _code = ce;
    }

    protected SCImpl(CS cs)
    {
        super(cs);
        _code = CEImpl.nullCE(NullFlavorImpl.NI);
    }

    public CE code()
    {
        if(isNull().isTrue())
            return CEImpl.nullCE(NullFlavorImpl.NI);
        else
            return _code;
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.SC;
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof SC))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            SC sc = (SC)any;
            BL bl = BLImpl.valueOf(stringValue().equals(sc.stringValue()));
            bl = code() == null || !code().isNull().isFalse() ? bl.and(BLImpl.valueOf(sc.code() == null || sc.code().isNull().isTrue())) : bl.and(code().equal(sc.code()));
            return bl;
        }
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
        {
            i = 37 * i + _value.hashCode();
            i = 37 * i + (_code != null ? _code.hashCode() : 0);
        }
        return i;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = null;
//            if(stringValue() != null)
//            {
//                xmlelement = (XMLElement)xmldocument.createElement(s);
//                if(code() != null && code().isNull().isFalse())
//                    xmlelement.setAttribute("code", code().code().stringValue());
//                String s1 = stringValue();
//                if(s1 != null)
//                {
//                    org.w3c.dom.Text text = xmldocument.createTextNode(s1);
//                    xmlelement.appendChild(text);
//                }
//            }
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static SC nullSC(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

	public byte[] byteArray() {
		// TODO Auto-generated method stub
		return null;
	}

}
